﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.BLL.Constants;

namespace BuXiaChu.OrderDishes.Web.Admin.Employees
{
    public partial class Add : System.Web.UI.Page
    {
        protected List<ShareCodeModel> genderList = null;
        protected List<AreaModel> cityList = null;
        protected List<ShareCodeModel> jobTileList = null;
        protected List<ServiceStationModel> serviceStationList = null;
        protected int saveStatus = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                InitData();
            }
            else
            {
                Save();
            }
        }

        private void InitData()
        {
            #region
            genderList = CodeDictionary.Instance.GetListByClassID(CodeDictionaryConstants.Gender);
            foreach (var item in genderList)
            {
                ListItem li = new ListItem(item.Name, item.CodeID.ToString());
                sel_Gender.Items.Add(li);
            }

            cityList = AreaManager.GetListByParentId(0);
            foreach (var item in cityList)
            {
                ListItem li = new ListItem(item.Name, item.Id.ToString());
                sel_City.Items.Add(li);
            }

            jobTileList = CodeDictionary.Instance.GetListByClassID(CodeDictionaryConstants.JobTitle);
            foreach (var item in jobTileList)
            {
                ListItem li = new ListItem(item.Name, item.CodeID.ToString());
                sel_JobTitle.Items.Add(li);
            }

            sel_ServiceStation.Items.Add(new ListItem("请选择", "-1"));

            #endregion

            #region
            var eId = 0;
            if (int.TryParse(Request.QueryString["id"], out eId))
            {
                EmployeesModel entity = EmployeesManager.GetEmployeesById(eId);
                txt_Name.Value = entity.Name;
                sel_Gender.Value = entity.Gender.ToString();
                sel_City.Value = entity.CityId.ToString();
                txt_AreaId.Value = entity.AreaId.ToString();
                sel_ServiceStation.Value = entity.ServiceStationId.ToString();
                sel_JobTitle.Value = entity.JobTitle.ToString();
                txt_Mobile.Value = entity.MobileTelephone;
                txt_Comment.Value = entity.Comment;
            }
            #endregion
        }

        private void Save()
        {
            #region
            var eId = 0;
            int.TryParse(Request.QueryString["id"], out eId);
            EmployeesModel entity = new EmployeesModel();
            entity.Id = eId;
            entity.Name = txt_Name.Value.Trim();
            entity.Gender = int.Parse(sel_Gender.Value);
            entity.CityId = int.Parse(sel_City.Value);
            entity.AreaId = int.Parse(txt_AreaId.Value);
            entity.ServiceStationId = int.Parse(sel_ServiceStation.Value);
            entity.JobTitle = int.Parse(sel_JobTitle.Value);
            entity.MobileTelephone = txt_Mobile.Value.Trim();
            entity.Comment = txt_Comment.Value.Trim();

            if (EmployeesManager.Save(entity))
            {
                saveStatus = 1;
            }
            else
            {
                saveStatus = 0;
            }
            #endregion
        }
    }
}
